package com.yxkj.syh.app.huarong.activities.creat.address.edit;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.syh.app.basic.base.BaseActivity;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.bean.EditOrderRequest;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityEditAddressBinding;
import com.yxkj.syh.app.huarong.views.AddressDialog;

/**
 * 修改地址 添加地址 编辑地址
 */
@Route(path = ArouterPath.EDIT_ADDRESS_ACTIVITY)
public class EditAddressActivity extends BaseActivity<ActivityEditAddressBinding, EditAddressVM> {

    private AddressDialog addressDialog;

    @Autowired
    EditOrderRequest request;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_edit_address;
    }

    @Override
    protected int getViewModelId() {
        return BR.editAddressVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (request !=null ){
            mViewModel.initVMData(request);
        }else {
            finish();
        }

        mVDBinding.titleView.setLeftOnClickListener(v -> {
            finish();
        });

        mVDBinding.llArea.setOnClickListener(clickListener);
        mVDBinding.etArea.setOnClickListener(clickListener);

    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (addressDialog == null) {
                addressDialog = new AddressDialog(EditAddressActivity.this);
                addressDialog.setOnAddressSelectedListener((province, city, county, street) -> {
                    addressDialog.cancel();
                    StringBuffer bufferName = new StringBuffer();
                    StringBuffer bufferCode = new StringBuffer();
                    bufferName.append(province.name);
                    bufferCode.append(mViewModel.getAreaPlusId(1, province.id));
                    if (city != null) {
                        bufferCode.append(",").append(mViewModel.getAreaPlusId(2, city.id));
                        bufferName.append("/").append(city.name);
                        if (county != null) {
                            bufferCode.append(",").append(mViewModel.getAreaPlusId(3, county.id));
                            bufferName.append("/").append(county.name);
                        }
                    }
                    mViewModel.ofForm3.set(bufferName.toString().replace("/",""));
                    mViewModel.ofForm3Source.set(bufferName.toString());
                    mViewModel.request.setAreaIds(bufferCode.toString());
                });
                addressDialog.getSelector().setAddressProvider(mViewModel.addressProvider);
            }
            addressDialog.show();
        }
    };

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldEditSuccess.observe(this, info -> {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("request", info);
            intent.putExtras(bundle);
            setResult(RESULT_OK,intent);
            finish();
        });
    }

    
    
    
}
